﻿using B.MESUI.Login.WareHouseManage.Dtos;
using DevExpress.XtraEditors;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace B.MESUI.Login.WareHouseManage.成品
{
    public partial class CreateMade_UpForm : DevExpress.XtraEditors.XtraForm
    {
        public delegate void Made_UpDelecate();
        public event Made_UpDelecate Made_UpEvent;
        public CreateMade_UpForm()
        {
            InitializeComponent();
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private async void simpleButton1_Click(object sender, EventArgs e)
        {
            //点击保存按钮触发保存方法 调用后端post请求添加仓库位置信息
            // 获取用户输入
            string Made_UpArticlesCode = textEdit1.Text.Trim(); // 假设你有这个控件
            string Made_UpArticlesName = textEdit2.Text.Trim();
            string Num = textEdit3.Text.Trim();
            string Material = textEdit4.Text.Trim();
            string Size = textEdit5.Text.Trim();
            string Package = textEdit6.Text.Trim();

            // 非空验证
            if (string.IsNullOrEmpty(Made_UpArticlesCode))
            {
                MessageBox.Show("成品编码不能为空");
                return;
            }
            if (string.IsNullOrEmpty(Made_UpArticlesName))
            {
                MessageBox.Show("成品名称不能为空");
                return;
            }
            if (string.IsNullOrEmpty(Num))
            {
                MessageBox.Show("数量不能为空");
                return;
            }
            if (string.IsNullOrEmpty(Material))
            {
                MessageBox.Show("材质不能为空");
                return;
            }
            if (string.IsNullOrEmpty(Size))
            {
                MessageBox.Show("尺寸不能为空");
                return;
            }
            if (string.IsNullOrEmpty(Package))
            {
                MessageBox.Show("包装不能为空");
                return;
            }

            // 构造请求体对象
            var model = new Made_UpInput
            {
                Made_UpArticlesCode = Made_UpArticlesCode,
                Made_UpArticlesName = Made_UpArticlesName,
                Num = Convert.ToInt32(Num),
                Material = Material,
                Size = Size,
                Package = Package
            };

            // 序列化为 JSON
            string jsonContent = JsonConvert.SerializeObject(model);
            var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");

            // 发起 POST 请求
            using (var client = new HttpClient())
            {
                try
                {
                    var response = await client.PostAsync("http://39.103.49.30:9981/api/Made_UpArticles/CreateMade_UpArticlesModel", content);

                    if (response.IsSuccessStatusCode)
                    {
                        MessageBox.Show("保存成功！");
                        this.DialogResult = DialogResult.OK;
                        this.Close(); // 关闭当前窗口
                        //这里使用委托获取父窗体的刷新方法
                        Made_UpEvent.Invoke();
                    }
                    else
                    {
                        string errorText = await response.Content.ReadAsStringAsync();
                        MessageBox.Show($"保存失败: {errorText}");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"发生错误: {ex.Message}");
                }
            }
        }

    }
}